﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Net.Mail
Imports System.Configuration

Partial Class Default2
    Inherits System.Web.UI.Page
    Dim quiz As New Quizsln
    Dim numCorrect As Double
    Dim randomQuestion As New DataTable
    Dim questionTotal As New Double

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If IsPostBack = False Then
            btnHome.Visible = False
            'Used to randomize the quiz questions
            Dim rand As New Random
            Dim questionId As New DataTable
            questionId = quiz.getQuizId()
            randomQuestion.Columns.Add("index")
            randomQuestion.Columns.Add("Random")
            For index As Integer = 1 To quiz.getQuizQuestionsCount()
                Dim dataRow As DataRow = randomQuestion.NewRow()
                dataRow(0) = questionId.Rows(index - 1)("Id")
                dataRow(1) = rand.Next(1000)
                randomQuestion.Rows.Add(dataRow)
                questionTotal += questionTotal
            Next
            Dim dataView As New DataView(randomQuestion)
            dataView.Sort = "Random"
            randomQuestion = dataView.ToTable
            Session("randomQuestion") = randomQuestion
        End If
        fillGridView()
    End Sub
    ''' <summary>
    ''' Creates dynamic label and radiobutton list representing each question and its answers for that question
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub fillGridView()
        For index As Integer = 1 To quiz.getQuizQuestionsCount()
            Dim tcLabel As New TableCell
            Dim tcRadioButtonList As New TableCell
            Dim questionLabel As Label = New Label
            Dim dynamicRbl As New RadioButtonList
            Dim tableRowForLabel As New TableRow
            Dim tableRowForRbl As New TableRow
            randomQuestion = Session("randomQuestion")

            'Creates label for the question
            Dim labelName As String
            labelName = "question" + index.ToString() + "lbl"
            questionLabel.ID = labelName
            questionLabel.Text = quiz.getQuizQuestions(Convert.ToInt32(randomQuestion.Rows(index - 1)("index")))
            questionLabel.Text = ("<br/><br?><b>" & questionLabel.Text & "<?b>")
            tcLabel.Controls.Add(questionLabel)

            'Creates dynamic radiobuttonlist for each group of answers
            Dim radioButtonName As String
            radioButtonName = "question" + index.ToString() + "RadioButtonList"
            dynamicRbl.ID = radioButtonName

            Dim quizAnswers As DataTable = New DataTable()
            quizAnswers = quiz.getQuizAnswers(Convert.ToInt32(randomQuestion.Rows(index - 1)("index")))
            For k As Integer = 0 To quizAnswers.Rows.Count - 1
                dynamicRbl.Items.Add(quizAnswers.Rows(k)(0).ToString())
            Next
            dynamicRbl.SelectedIndex = 0
            tcRadioButtonList.Controls.Add(dynamicRbl)
            tableRowForLabel.Cells.Add(tcLabel)
            tblQuiz.Rows.Add(tableRowForLabel)
            tableRowForRbl.Cells.Add(tcRadioButtonList)

            tblQuiz.Rows.Add(tableRowForRbl)
        Next
        Session("tblQuiz") = tblQuiz
    End Sub

    Protected Sub submitButton_Click(sender As Object, e As EventArgs) Handles submitButton.Click

        'stores how many the student got correct from the quiz
        For index As Integer = 1 To quiz.getQuizQuestionsCount()
            Dim radioButtonName As String
            radioButtonName = "question" + index.ToString() + "RadioButtonList"
            Dim table As Table = Session("tblQuiz")
            randomQuestion = Session("randomQuestion")
            Dim radioButtonList As RadioButtonList = table.FindControl(radioButtonName)
            'Dim radioButtonList As RadioButtonList = DirectCast(t.FindControl(radioButtonName), RadioButtonList)
            Dim correctAnswer As DataTable = New DataTable()
            correctAnswer = quiz.getCorrectAnswer(Convert.ToInt32(randomQuestion.Rows(index - 1)("index")))
            For correct As Integer = 1 To correctAnswer.Rows.Count
                If (radioButtonList.SelectedValue = correctAnswer.Rows(correct - 1)("Answer").ToString()) Then
                    numCorrect = numCorrect + 1
                End If
            Next
            
        Next
        tblQuiz.Rows.Clear()
        submitButton.Visible = False
        Dim studentAttempt As Integer = Session("studentAttempt")
        Dim bannerID As String = Session("bannerId")
        Dim email As String = Session("email")
        correctCheck(studentAttempt, bannerID, email)


    End Sub
    ''' <summary>
    ''' emails the CSSA van management liaisons email address notifying them who passed/failed
    ''' </summary>
    ''' <param name="pass"></param>
    ''' <remarks></remarks>
    Protected Sub sendEmail(ByVal pass As Boolean)

        Dim bannerId As String = Session("bannerId")
        Dim email As String = Session("email")
        Dim name As String
        name = quiz.getFnLn(bannerId)

        Dim Mail As New MailMessage
        Dim SMTP As New SmtpClient("smtp.gmail.com")

        Mail.Subject = "CSSA VAN MANAGEMENT QUIZ"
        Mail.From = New MailAddress("cssaliaisons@jcu.edu")
        SMTP.Credentials = New System.Net.NetworkCredential("cssaliaisons@jcu.edu", "driver2012") '<-- Password Here

        Mail.To.Add("cssaliaisons@jcu.edu") 'Use liason email as well as the emails for CSSA ladies
        Mail.CC.Add("tbakula@jcu.edu")
        Mail.CC.Add("mcallanan@jcu.edu")
        If (pass) Then
            Mail.Body = name & " passed" 'Pull person's info (email, banner, name) from session variables and include them in email
            ' Subject of email 
        Else
            Mail.Body = name & " failed"
            ' Same as above, but include how many attempts the person has remnaining.
        End If


        SMTP.EnableSsl = True
        SMTP.Port = "587"
        SMTP.Send(Mail)


    End Sub
    ''' <summary>
    ''' Sets the label at the top of the page with the pass % 
    ''' </summary>
    ''' <param name="studentAttempt"></param>
    ''' <param name="bannerId"></param>
    ''' <param name="email"></param>
    ''' <remarks></remarks>
    Protected Sub correctCheck(ByVal studentAttempt As Integer, ByVal bannerId As String, ByVal email As String)


        Dim quizGrade As New Double
        quizGrade = (numCorrect / randomQuestion.Rows.Count)
        If (quizGrade > 0.8) Then
            If (studentAttempt = -1) Then
                quiz.insertQuizStudents(bannerID, email, "Y")
            End If

            If (studentAttempt = 1) Then
                quiz.UpdateQuizStudents(bannerID, "Y")
            End If
            passFailLabel.Text = "<b>You passed the quiz with " & quizGrade * 100 & " percent correct, <br/>and are now certified to drive Vans for CSSA. </b>"
            sendEmail(True)
            btnHome.Visible = True
        Else
            If (studentAttempt = -1) Then
                quiz.insertQuizStudents(bannerID, email, "N")
            End If

            If (studentAttempt = 1) Then
                quiz.UpdateQuizStudents(bannerID, "N")
            End If
            passFailLabel.Text = "<b>You failed the quiz with " & quizGrade * 100 & " percent correct, </b>" ''Have it check for the number of attempts remaining here
            sendEmail(False)
            btnHome.Visible = True
        End If
    End Sub

    Protected Sub btnHome_Click(sender As Object, e As EventArgs) Handles btnHome.Click
        Response.Redirect("Default.aspx")
    End Sub
End Class